package net.pinnoocle.playshoppc.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.pinnoocle.playcommon.response.BaseResult;
import net.pinnoocle.playdao.entity.WechatCommit;
import net.pinnoocle.playdao.entity.WechatSubmit;
import net.pinnoocle.playdao.entity.wechat.*;
import net.pinnoocle.playservice.service.impl.RegisterWechatServiceImpl;
import net.pinnoocle.playservice.service.impl.UserServiceImpl;
import net.pinnoocle.playservice.service.impl.WechatServiceImpl;
import net.pinnoocle.playservice.service.impl.WxServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ye
 * @Description
 * @create 2021-11-19 10:54
 */
@RestController
@RequestMapping("/wx")
@Api(tags = "微信第三方平台相关")
public class WechatCodeController {

    @Autowired
    WechatServiceImpl wechatService;
    @Autowired
    WxServiceImpl wxService;
    @Autowired
    RegisterWechatServiceImpl registerWechatService;
    @Autowired
    UserServiceImpl userService;

    @GetMapping("basicInfo")
    @ApiOperation("小程序基本信息查看")
    public BaseResult<WeChatBasicInfoVo> basicInfo(){
        return new BaseResult<WeChatBasicInfoVo>().ok(wechatService.basicInfoVo());
    }

    @GetMapping("getTemplateList")
    @ApiOperation("模板库列表")
    public BaseResult<List<TemplateListVo>> getTemplateList(){
        List<TemplateListVo> templateList = wechatService.templateList();
        return new BaseResult<List<TemplateListVo>>().ok(templateList);
    }

    @PostMapping("commitCode")
    @ApiOperation("上传代码")
    public BaseResult<String> commitCode(@RequestBody CommitCodeParams params){
        boolean result = wechatService.commitCode(params);
        if(result){
            return new BaseResult<String>().success("上传代码成功");
        }
        return new BaseResult<String>().error("上传代码失败");
    }

    @GetMapping("wxPages")
    @ApiOperation("获取已上传的代码的页面列表(暂时不需要)")
    public BaseResult<List<String>> wxPages(){
        return new BaseResult<List<String>>().ok(wechatService.getPages());
    }

    @GetMapping("getCategory")
    @ApiOperation("获取审核时可填写的类目信息(暂时不需要)")
    public BaseResult<List<WechatCategoryList>> getCategory(){
        return new BaseResult<List<WechatCategoryList>>().ok(wechatService.wxCategory());
    }

    @GetMapping("refreshToken")
    @ApiOperation("获取令牌(上传素材时)")
    public BaseResult<String> refreshToken(){
        return new BaseResult<String>().ok(wechatService.refreshToken());
    }

    @GetMapping("getComponentAccessToken")
    @ApiOperation("获取第三方平台令牌(测试用，前端忽略)")
    public BaseResult<String> getComponentAccessToken(){
        return new BaseResult<String>().ok(wxService.getComponentAccessToken());
    }

    @PostMapping("submitAudit")
    @ApiOperation("提交审核")
    public BaseResult<String> submitAudit(@RequestBody SubmitAuditParams params){
        boolean result = wechatService.submitAudit(params);
        if(result){
            return new BaseResult<String>().success("提交成功");
        }
        return new BaseResult<String>().error("提交失败");
    }

    @GetMapping("commitRecord")
    @ApiOperation("提交代码记录")
    public BaseResult<WechatCommit> commitRecord(){
        return new BaseResult<WechatCommit>().ok(wechatService.commitRecord());
    }

    @GetMapping("submitRecord")
    @ApiOperation("提交审核记录")
    public BaseResult<WechatSubmit> submitRecord(){
        return new BaseResult<WechatSubmit>().ok(wechatService.submitRecord());
    }

    @GetMapping("releaseWechat")
    @ApiOperation("发布已审核通过的小程序")
    public BaseResult<String> releaseWechat(){
        boolean result = wechatService.releaseWechat();
        if(result){
            return new BaseResult<String>().success("发布成功");
        }
        return new BaseResult<String>().error("发布失败");
    }

    @GetMapping("setHeadImage")
    @ApiOperation("设置小程序头像")
    public BaseResult<String> setHeadImage(@RequestParam("mediaId") String mediaId){
        boolean result = wechatService.setHeadImage(mediaId);
        if(result){
            return new BaseResult<String>().success("头像设置成功");
        }
        return new BaseResult<String>().error("头像设置失败");
    }

    @PostMapping("setNickName")
    @ApiOperation("设置小程序名称(只能改2次)")
    public BaseResult<String> setNickName(@RequestBody SaveWechatNickNameParams params){
        boolean result = wechatService.setNickName(params);
        if(result){
            return new BaseResult<String>().success("昵称设置成功");
        }
        return new BaseResult<String>().error("昵称设置失败");
    }

    @GetMapping("setSignature")
    @ApiOperation("设置小程序功能简介")
    public BaseResult<String> setSignature(@RequestBody SaveWechatSignatureParams params){
        boolean result = wechatService.setSignature(params);
        if(result){
            return new BaseResult<String>().success("简介成功");
        }
        return new BaseResult<String>().error("简介失败");
    }

    @GetMapping("addCategory")
    @ApiOperation("添加小程序类目(暂时下拉框:休闲娱乐)")
    public BaseResult<String> addCategory(){
        boolean result = wechatService.addCategory();
        if(result){
            return new BaseResult<String>().success("类目添加成功");
        }
        return new BaseResult<String>().error("类目添加失败");
    }

    @PostMapping("setPrivacySetting")
    @ApiOperation("配置小程序用户隐私保护指引")
    public BaseResult<String> setPrivacySetting(@RequestBody SetPrivacySettingParams params){
        String shopId = userService.getGlobalShopId();
        boolean result = wechatService.setPrivacySetting(params,shopId);
        if(result){
            return new BaseResult<String>().success("配置隐私保护指引成功");
        }
        return new BaseResult<String>().error("配置隐私保护指引失败");
    }

    @PostMapping("/getMediaId")
    @ApiOperation("图片上传微信")
    public BaseResult<String> getMediaId(@RequestParam("file") MultipartFile file) throws Exception {
        return new BaseResult<String>().ok(wechatService.getMediaId(file));
    }
}
